Java.perform(function () {
    console.log("Combined script loaded successfully");
    try {
        // 获取 WebView 类
        var WebView = Java.use('android.webkit.WebView');
        var WebSettings = Java.use('android.webkit.WebSettings');
        var CookieManager = Java.use('android.webkit.CookieManager');
        var HttpUrl = Java.use('okhttp3.HttpUrl');  // 用于解析 URL
        var sent = false;

        // 拦截 WebView.loadUrl 方法
        WebView.loadUrl.overload('java.lang.String').implementation = function (url) {
//            console.log("Intercepted WebView URL: " + url);

            // 仅针对特定的 URL 进行处理
            if (url.startsWith('https://xyks.yuanfudao.com/bh5/leo-web-oral-pk/pk.html?')) {
                if (!sent) {  // 确保只发送一次
//                    console.log("Intercepted specific URL in WebView: " + url);

                    // 使用 HttpUrl 解析 URL
                    var parsedUrl = HttpUrl.parse(url);
                    if (parsedUrl) {
                        var yfdUFromUrl = parsedUrl.queryParameter('YFD_U');

                        // 获取 User-Agent
                        var webSettings = this.getSettings(); // 获取 WebSettings
                        var userAgentFromHeader = webSettings.getUserAgentString();
//                        console.log("User-Agent: " + userAgentFromHeader);

                        // 获取 Cookie
                        var cookieManager = CookieManager.getInstance();
                        var originalCookieFromHeader = cookieManager.getCookie(url);
//                        console.log("Cookie: " + originalCookieFromHeader);

                        // 发送数据
                        send({
                            url: url,
                            yfdU: yfdUFromUrl,
                            originalCookie: originalCookieFromHeader,
                            userAgent: userAgentFromHeader,
                        });
                        sent = true;  // 设置标志，避免多次发送
                    } else {
                        console.log("Failed to parse URL");
                    }
                }
            }

            // 调用原始 loadUrl 方法
            return this.loadUrl(url);
        };

//        console.log("Started intercepting WebView.loadUrl for specific URL");

    } catch (err) {
        console.log("WebView not found: " + err);
        console.log(err.stack);  // 打印详细错误堆栈
    }
});
